package user_interaction;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import dca.BadDataException;
import dca.Cure;
import dca.Disease;
import dca.Symptom;
import dca.DiseaseRegistry;
import dca.SymptomRegistry;


public class Disease_GUI extends JFrame {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final int FRAME_WIDTH = 600;
	private static final int FRAME_HEIGTH = 500;
	
	private JTextArea display;
	private JScrollPane dispalyScrollpane;
	private JTextField inputField;
	private JComboBox<?> cureType;
	private JTextField cureName;
	private JTextField cureHealFactor;
	private JComboBox<?> symptom1;
	private JComboBox<?> symptom2;
	private JComboBox<?> symptom3;
	private JComboBox<?> symptom4;	
	private JButton AddButton;
	
	private DiseaseRegistry DiseaseBase;
	private SymptomRegistry SymptomBase;
		
	public Disease_GUI(DiseaseRegistry DiseaseBase,SymptomRegistry SymptomBase){
		this.DiseaseBase = DiseaseBase;
		this.SymptomBase = SymptomBase;
		
		
		//add visual components
		
		display = new JTextArea(15,50);
		display.setToolTipText("Main display");
		dispalyScrollpane = new JScrollPane(display);
		
		inputField = new JTextField(10);
		inputField.setToolTipText("Disease Name");
		
		String[] cureTypesString = {"Pill","Injection","Surgery"};
		cureType = new JComboBox<Object>(cureTypesString);
		cureType.setToolTipText("Cure Type");
		cureName = new JTextField(10);
		cureName.setToolTipText("Cure name");
		cureHealFactor = new JTextField(2);
		cureHealFactor.setToolTipText("Cure effeciency(%p)");
		
		//Finds the existing symptoms for dropDown boxes
		String [] symptomsString = new String[SymptomBase.SymptomList().size()+1];
		symptomsString[0]="N/A";
		int i=1;
		for (Symptom s: SymptomBase.SymptomList())
		{
			symptomsString[i]=s.getName();
			i++;
		}		
		symptom1 = new JComboBox<Object>(symptomsString);;
		symptom1.setToolTipText("Symptoms");
		symptom2 = new JComboBox<Object>(symptomsString);;
		symptom3 = new JComboBox<Object>(symptomsString);;
		symptom4 = new JComboBox<Object>(symptomsString);;		
		AddButton = new JButton("Add Disease");
		AddButton.addActionListener(new AddButtonListener());
		setLayout(new FlowLayout());
		setSize(FRAME_WIDTH,FRAME_HEIGTH);
		add(dispalyScrollpane);
		add(inputField);
		add(cureType);
		add(cureName);
		add(cureHealFactor);
		add(symptom1);
		add(symptom2);
		add(symptom3);
		add(symptom4);
		add(AddButton);
		

		//Add content to display
		try {
			updateDisplay();
		} catch (BadDataException e) {
			System.out.println(e.getMessage());
		}
		
	}
	
	public void updateDisplay() throws BadDataException{
		String DiseaseList="";
		for(Disease s:DiseaseBase.DiseaseList())
		{
			DiseaseList+=s.toString()+"\nDecease classification: "+s.getSeverity().toString()+"\n\n";
		}
		display.setText(DiseaseList);

		
	}
	
	
	class AddButtonListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0)  {
			String name = inputField.getText();
			ArrayList<Symptom> symptoms = new ArrayList<Symptom>();
			if(symptom1.getSelectedIndex()>0)
				symptoms.add(SymptomBase.getSymptom(symptom1.getSelectedItem().toString()));
			if(symptom2.getSelectedIndex()>0)
				symptoms.add(SymptomBase.getSymptom(symptom2.getSelectedItem().toString()));
			if(symptom3.getSelectedIndex()>0)
				symptoms.add(SymptomBase.getSymptom(symptom3.getSelectedItem().toString()));
			if(symptom4.getSelectedIndex()>0)
				symptoms.add(SymptomBase.getSymptom(symptom4.getSelectedItem().toString()));
			
			//This grabs the cure
			int healFactor = Integer.parseInt(cureHealFactor.getText().trim());
			Cure cure = DiseaseBase.findCure(cureType.getSelectedItem().toString(), cureName.getText(),healFactor);
			
			
			try {
				DiseaseBase.addDisease(name, symptoms, cure);
				DiseaseBase.saveDiseases();
				updateDisplay();

			} catch (FileNotFoundException | BadDataException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 

			
			
			 
			
		}
		
	}
	

}
